import 'package:flutter/material.dart';
import 'package:animation/generated/l10n.dart';

class NormalInput extends StatelessWidget {
  final TextEditingController controller;
  final Icon? icon;
  final String? title;
  final double? height;
  final double radius;
  final void Function(String)? onSubmitted;
  final bool showClear;
  const NormalInput(
      {super.key,
      required this.controller,
      this.icon,
      this.title = '',
      this.height = 48,
      this.onSubmitted,
      this.showClear = true,
      this.radius = 20});

  OutlineInputBorder _buildCustomBorder() {
    return OutlineInputBorder(
      borderRadius: BorderRadius.circular(radius),
      borderSide: BorderSide.none, // 关键设置
    );
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: height,
      child: ValueListenableBuilder<TextEditingValue>(
        valueListenable: controller,
        builder: (context, value, child) {
          return TextFormField(
            controller: controller,
            style: TextStyle(
              fontWeight: FontWeight.w400,
              height: 1,
            ),
            onFieldSubmitted: onSubmitted,
            decoration: InputDecoration(
              hintText: '${S.of(context).a7}$title',
              hintStyle: TextStyle(color: Colors.grey[500]),
              prefixIcon: icon,
              prefixIconColor: Colors.grey[500],
              fillColor: Theme.of(context).colorScheme.secondary,
              filled: true,
              border: _buildCustomBorder(),
              contentPadding: EdgeInsets.symmetric(
                vertical: height! * 0.3,
                horizontal: 16,
              ),
              // 新增清除按钮
              suffixIcon: showClear && value.text.isNotEmpty
                  ? IconButton(
                      icon: Icon(
                        Icons.clear,
                        color: Colors.grey[500],
                        size: 20,
                      ),
                      onPressed: () => controller.clear(),
                    )
                  : null,
            ),
          );
        },
      ),
    );
  }
}
